#include "MainGameScene.h"
#include "GameUtil.h"


using namespace cocos2d;

MainGameScene::~MainGameScene(void)
{
	_enemyManager->~EnemyManager();
	_enemyManager = NULL;

	GameUtil::instance()->~GameUtil();
}

CCScene* MainGameScene::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        MainGameScene *layer = new MainGameScene();
		if (layer && layer->init(scene)) {
			layer->autorelease();

		} else {
			delete layer;
			layer = NULL;
		}

        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);


    } while (0);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool MainGameScene::init(CCScene* scene)
{
    bool bRet = false;
	GameUtil::instance()->setGameLayer(this);
	GameUtil::instance()->setGameScene(scene);
    do 
    {
        CC_BREAK_IF(! CCLayer::init());

		_winSize = CCDirector::sharedDirector()->getWinSize(); 

		// init game
		_enemyManager = new EnemyManager(this);
		GameUtil::instance()->setEnemyManager(_enemyManager);

		_bulletManager = new BulletManager(this);
		GameUtil::instance()->setBulletManager(_bulletManager);

		_toothManager = new ToothManager(this);
		GameUtil::instance()->setToothManager(_toothManager);

		_waveManager = new WaveManager(this);
		GameUtil::instance()->setWaveManager(_waveManager);

		_gameStateLayer = GameStateLayer::create();
		this->addChild(_gameStateLayer, 1);

		GameUtil::instance()->setGameStateLayer(_gameStateLayer);

		// enable touch
		this->setTouchEnabled(true);
		this->setKeypadEnabled(true);
		d_touchID = NULL;

		// background
        CCSprite* bg = CCSprite::create("scene_002.png");
        CC_BREAK_IF(! bg);
		//bg->setAnchorPoint(ccp(0, 0));
  //      bg->setPosition(ccp(0, 0));
		bg->setPosition(ccp(_winSize.width/2, _winSize.height/2));

		bg->setScale(1.5f);
		//bg->setScaleY(GameUtil::instance()->getSetting()->getFieldSize().height / bg->getContentSize().height);  // TODO
        this->addChild(bg, 0);

		// add update
		this->schedule(schedule_selector(MainGameScene::update));

		// add score
		//_score = new Score(this);

		d_Player = Player::create();
		GameUtil::instance()->setPlayer(d_Player);
		this->addChild(d_Player);

		Shield* shield = Shield::create();
		GameUtil::instance()->setShield(shield);
		this->addChild(shield);

		
		// start wave
		_waveManager->startWave();

        bRet = true;
    } while (0);

    return bRet;
}

void MainGameScene::ccTouchesBegan(CCSet* pTouches,CCEvent* pEvent)
{
	CCTouch* touch;
	if(d_touchID == NULL) {
		touch = (CCTouch*)pTouches->anyObject();
		d_touchID = touch->getID();
		d_Player->setTouchStart(touch->getLocation().y);
	} else {
		CCSetIterator it = pTouches->begin();
		CCPoint pt;

		for(int iTouchCount = 0; iTouchCount < pTouches->count(); iTouchCount++)
		{
			touch = (CCTouch*)(*it);

			if(touch->getID() == d_touchID) {
				d_Player->setTouchStart(touch->getLocation().y);
				return;
			}
			it++;
		}
	}
}

void MainGameScene::ccTouchesMoved(CCSet* pTouches,CCEvent* pEvent)
{
	ccTouchesBegan(pTouches, pEvent);
}

void MainGameScene::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{    
	CCSetIterator it = pTouches->begin();
    CCPoint pt;
    CCTouch* touch;

    for(int iTouchCount = 0; iTouchCount < pTouches->count(); iTouchCount++)
    {
        touch = (CCTouch*)(*it);

		if(touch->getID() == d_touchID) {
			d_touchID = NULL;
			d_Player->setTouchEnd();
			return;
		}
        it++;
    }
}


void MainGameScene::update(float dt)
{
	_bulletManager->checkCollision();
	d_Player->update(dt);
}

void MainGameScene::keyBackClicked()
{
	CCDirector::sharedDirector()->end();
}